package org.lucky.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lucky.admin.query.InviteQuery;
import org.lucky.admin.query.UserQuery;
import org.lucky.admin.service.IInviteService;
import org.lucky.admin.util.ExcelUtil;
import org.lucky_common.domain.Admin;
import org.lucky_common.domain.InviteParam;
import org.lucky_common.domain.User;
import org.lucky_common.util.AjaxResult;
import org.lucky_common.util.PageList;
import org.lucky_common.vo.InviteExportVo;
import org.lucky_common.vo.UserExportVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/invite")
public class AdminInviteController {
    @Resource
    private IInviteService inviteService;

    @PutMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody InviteParam inviteParam) {
        boolean b = inviteService.saveOrUpdate(inviteParam);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error saving employee");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        boolean b = inviteService.removeById(id);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error");
        }
    }

    @DeleteMapping("/deleteList")
    public AjaxResult deleteList(@RequestBody ArrayList<Long> ids) {
        boolean b = inviteService.removeByIds(ids);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error");
        }
    }

    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(inviteService.getById(id));
    }

    @PostMapping
    public AjaxResult queryPage(@RequestBody InviteQuery userQuery) {
        Page<InviteParam> page = new Page<InviteParam>(userQuery.getPage(),userQuery.getRows(),true);

        if (userQuery.getStart().equals("") && userQuery.getEnd().equals("")){
            page = inviteService.page(page,new QueryWrapper<InviteParam>().like("invitedCode",userQuery.getKeyword()).orderByDesc("data"));
            PageList<InviteParam> pageList = new PageList<InviteParam>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);
        }else {
            page = inviteService.page(page,new QueryWrapper<InviteParam>().like("invitedCode",userQuery.getKeyword()).between("data",userQuery.getStart(),userQuery.getEnd()));
            PageList<InviteParam> pageList = new PageList<InviteParam>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);
        }
    }

    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        //查询要导出的数据
        List<InviteExportVo> users = inviteService.getUserExportList();
        InviteExportVo.threadLocal_collectionSize.set( users.size() );
        InviteExportVo.threadLocal_insertIndex.set( 1 );
        ExcelUtil.exportExcelX(users, "邀请阶段二信息表", "sheet1", InviteExportVo.class, "邀请阶段二信息表.xlsx", response);
    }

    @PutMapping("/exportMore")
    public void exportMore11(@RequestBody ArrayList<InviteExportVo> data, HttpServletResponse response) {
        List<InviteExportVo> userExportVo = new ArrayList<>(data);
        InviteExportVo.threadLocal_collectionSize.set( userExportVo.size() );
        InviteExportVo.threadLocal_insertIndex.set( 1 );
        ExcelUtil.exportExcelX(userExportVo, "邀请阶段二信息表", "sheet1", InviteExportVo.class, "邀请阶段二信息表.xlsx", response);
    }

    @GetMapping("/getCount")
    public AjaxResult getCount() {
        Map<String,List> count = inviteService.getCount();
        return AjaxResult.success(count);
    }
}
